package com.example.demo;

import com.example.demo.entity.Course;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TestStudentCourseLazy {
    public static void main(String[] args) {
        SqlSession session = MyBatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);

        // 只查主表，不查课程（子查询未触发）
        List<Student> studentList = mapper.getAllStudents();

        System.out.println("===== 懒加载演示（初始只查学生） =====");
        for (Student s : studentList) {
            System.out.println("学生：" + s.getName());
        }

        System.out.println("===== 手动访问 course 属性，触发懒加载 =====");
        for (Student s : studentList) {
            System.out.println("学生：" + s.getName());
            for (Course c : s.getCourses()) { // ⚠️ 触发懒加载
                System.out.println("  - 课程：" + c.getName());
            }
        }

        session.close();
    }
}